/*
 * Copyright (c) 2025 CAX Conversion Project
 */
package com.comac.ins.isfd.service.impl;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.comac.ins.common.core.domain.model.LoginUser;
import com.comac.ins.common.core.utils.MapstructUtils;
import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.satoken.utils.LoginHelper;
import com.comac.ins.isfd.domain.IsfdDatasourceTaskFieldMapping;
import com.comac.ins.isfd.domain.bo.IsfdDatasourceTaskFieldMappingBo;
import com.comac.ins.isfd.domain.vo.IsfdDatasourceTaskFieldMappingVo;
import com.comac.ins.isfd.mapper.IsfdDatasourceTaskFieldMappingMapper;
import com.comac.ins.isfd.service.IIsfdDatasourceTaskFieldMappingService;
import lombok.RequiredArgsConstructor;

/**
 * 数据源数据集成任务字段映射Service业务层处理
 *
 * @author Lion Li
 * @date 2024-10-11
 */
@RequiredArgsConstructor
@Service
public class IsfdDatasourceTaskFieldMappingServiceImpl implements IIsfdDatasourceTaskFieldMappingService {

    private final IsfdDatasourceTaskFieldMappingMapper baseMapper;

    /**
     * 查询数据源数据集成任务字段映射
     *
     * @param id 主键
     * @return 数据源数据集成任务字段映射
     */
    @Override
    public IsfdDatasourceTaskFieldMappingVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询数据源数据集成任务字段映射列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 数据源数据集成任务字段映射分页列表
     */
    @Override
    public TableDataInfo<IsfdDatasourceTaskFieldMappingVo> queryPageList(IsfdDatasourceTaskFieldMappingBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IsfdDatasourceTaskFieldMapping> lqw = buildQueryWrapper(bo);
        Page<IsfdDatasourceTaskFieldMappingVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的数据源数据集成任务字段映射列表
     *
     * @param bo 查询条件
     * @return 数据源数据集成任务字段映射列表
     */
    @Override
    public List<IsfdDatasourceTaskFieldMappingVo> queryList(IsfdDatasourceTaskFieldMappingBo bo) {
        LambdaQueryWrapper<IsfdDatasourceTaskFieldMapping> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IsfdDatasourceTaskFieldMapping> buildQueryWrapper(IsfdDatasourceTaskFieldMappingBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IsfdDatasourceTaskFieldMapping> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTargetId() != null, IsfdDatasourceTaskFieldMapping::getTargetId, bo.getTargetId());
        lqw.eq(StringUtils.isNotBlank(bo.getSourceField()), IsfdDatasourceTaskFieldMapping::getSourceField, bo.getSourceField());
        lqw.like(StringUtils.isNotBlank(bo.getSourceTableName()), IsfdDatasourceTaskFieldMapping::getSourceTableName, bo.getSourceTableName());
        lqw.eq(StringUtils.isNotBlank(bo.getTargetField()), IsfdDatasourceTaskFieldMapping::getTargetField, bo.getTargetField());
        lqw.like(StringUtils.isNotBlank(bo.getTargetTableName()), IsfdDatasourceTaskFieldMapping::getTargetTableName, bo.getTargetTableName());
        lqw.eq(StringUtils.isNotBlank(bo.getFieldType()), IsfdDatasourceTaskFieldMapping::getFieldType, bo.getFieldType());
        lqw.eq(bo.getFieldLength() != null, IsfdDatasourceTaskFieldMapping::getFieldLength, bo.getFieldLength());
        lqw.eq(bo.getIsDeleted() != null, IsfdDatasourceTaskFieldMapping::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

    /**
     * 新增数据源数据集成任务字段映射
     *
     * @param bo 数据源数据集成任务字段映射
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(IsfdDatasourceTaskFieldMappingBo bo) {
        IsfdDatasourceTaskFieldMapping add = MapstructUtils.convert(bo, IsfdDatasourceTaskFieldMapping.class);
        validEntityBeforeSave(add);
        LoginUser loginUser = LoginHelper.getLoginUser();
        bo.setCreateBy(loginUser.getUserId());
        bo.setUpdateBy(loginUser.getUserId());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改数据源数据集成任务字段映射
     *
     * @param bo 数据源数据集成任务字段映射
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(IsfdDatasourceTaskFieldMappingBo bo) {
        IsfdDatasourceTaskFieldMapping update = MapstructUtils.convert(bo, IsfdDatasourceTaskFieldMapping.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IsfdDatasourceTaskFieldMapping entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除数据源数据集成任务字段映射信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
